import { request } from '../../utils/request'
import { request_netEase } from '../../utils/request_netEase'
let that
Page({
  /**
   * 页面的初始数据
   */
  data: {
    //当前日期
    date:{},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    that = this
    const userInfo = wx.getStorageSync('userInfo');
    if(userInfo === '') wx.showToast({
      title:'请先登录',
      icon:'none',
      duration:1000,
      success: () => wx.reLaunch({
        url:'/pages/login/login',
      })
    })
    that.getDate()
    that.getRecommend()
  },
  getDate: () => {
    const now = new Date()
    const day = now.getDate()
    const month = now.getMonth()+1
    const date = {
      day,
      month
    }
    that.setData({
      date
    })
  },
  getRecommend: async () => {
    const songData = await request_netEase('getRecommendList')
    const { recommendList_info: { recommendList: recommendSongs} } = songData
    that.setData({
      recommendSongs
    })
    // try{ 
    //   wx.setStorageSync(
    //     'recommendSongs', recommendSongs
    //     );
    //   }catch(e) {
    //     console.log(e)
    //   }
  },
  handleSongDetail: (e) => {
    //增加index下标数据, 可以减少后面查询歌曲index的步骤, 减少消耗
    const { song, index } = e.target.dataset
    //将歌曲json文件转换为字符串, 并且encode防止字符串中特殊字符污染
    //小程序对字符串长度有限制, 会截取导致JSON.parse()报错
    const songString = JSON.stringify(song)
    const encodeSongString = encodeURIComponent(songString)
    wx.navigateTo({
      //多个参数用'&'连接
      url:`/pages/songDetail/songDetail?song=${encodeSongString}&index=${index}`,
      success: () => {
        wx.showLoading({
          title:'歌曲加载中'
        })
        setTimeout(() => {
          wx.hideLoading()
        }, 300)
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})